package com.atguigu04.set;

import org.junit.Test;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class TreeSet1 {
    public static void main(String[] args) {
        TreeSet set=new TreeSet();
        User u1=new User("tom",12);
        User u2=new User("jack",12);
        User u3=new User("tim",20);
        User u4=new User("xiaoyu",89);

        set.add(u1);
        set.add(u2);
        set.add(u3);
        set.add(u4);
      //  set.add(u5);

        Iterator iterator=set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }

    }

    /*
    * 定制排序
    * */
    @Test
    public void t(){

        Comparator comparator=new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if(o1==o2){
                    return 0;
                }
                if(o1 instanceof User&&o2 instanceof User ){
                    User u1=(User)o1;
                    User u2=(User)o2;
                    return u1.age-u2.age;
                }
                throw new RuntimeException("");
            }
        };
        TreeSet set=new TreeSet(comparator);
        User u1=new User("tom",12);
        User u2=new User("jack",8);
        User u3=new User("tim",20);
        User u4=new User("xiaoyu",89);

        set.add(u1);
        set.add(u2);
        set.add(u3);
        set.add(u4);

        Iterator iterator=set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }

    }


}
